package com.sumainfor.wx.system.gmio.service;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.wx.system.feign.BaseFeignMarket;
import com.sumainfor.wx.system.gmio.dao.TenantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Meng
 * @ClassName LookOverHouseService
 * @date 2019/8/15 11:13
 * @description: TODO
 */
@Service
public class LookOverHouseService {

    @Autowired
    private BaseFeignMarket feignMarket;

    @Autowired
    private TenantMapper tenantMapper;

    /**
     * @Description //TODO 获取管家巡房记录
     * @return com.sumainfor.common.util.JsonResult
     * @Param [params, pager]
     * @Date 2019/8/15 16:34
     **/
    public JsonResult getUnLookOverHouseList(Map<String, Object> params, Pager pager) {

        return feignMarket.getHousingPatrolList(params, pager);
    }

    /**
     * @Description //TODO 服务管家增加巡房记录
     * @return com.sumainfor.common.util.JsonResult
     * @Param [params]
     * @Date 2019/8/15 17:53
     **/
    public JsonResult addLookOver(Map<String, Object> params) {

        //取出房源id和管理员id
        String houId = (String) params.get("HouId");
        String userId = ContextUtils.getUserId();
        Integer no = tenantMapper.getHouseNoByHouseIdAndSysId(houId, userId);
        if (no == 0) {
            return JsonResult.error("无权限操作此房源");
        }
        return feignMarket.AddPatrol(params);
    }

    /**
     * @Description //TODO 获取租客违约分类
     * @return com.sumainfor.common.util.JsonResult
     * @Param []
     * @Date 2019/8/15 18:12
     **/
    public JsonResult getViolationList() {

        return feignMarket.getViolationList();
    }

    /**
     * @Description //TODO 根据租客违规分类编号获取租客违规内容
     * @return com.sumainfor.common.util.JsonResult
     * @Param [params]
     * @Date 2019/8/15 18:13
     **/
    public JsonResult getViolationInfoList(Map<String, Object> params) {

        return feignMarket.getViolationInfoList(params);
    }

    /**
     * @Description //TODO 根据房源编号获取巡房资料
     * @return com.sumainfor.common.util.JsonResult
     * @Param [params]
     * @Date 2019/8/15 18:15
     **/
    public JsonResult getPatrolHousginMap(Map<String, Object> params) {
        return feignMarket.getPatrolHousginMap(params);
    }

    /**
     * @Description //TODO 根据房源主编号获取当前房源历史租客违规列表
     * @return com.sumainfor.common.util.JsonResult
     * @Param [params, pager]
     * @Date 2019/8/15 18:17
     **/
    public JsonResult getPatrolHousingViolationList(Map<String, Object> params, Pager pager) {
        return feignMarket.getPatrolHousingViolationList(params, pager);
    }

    /**
     * @Description //TODO 根据房源主编号获取当前房源历史租客违规列表
     * @return com.sumainfor.common.util.JsonResult
     * @Param [params, pager]
     * @Date 2019/8/15 18:18
     **/
    public JsonResult getPatrolHousingCleaningList(Map<String, Object> params, Pager pager) {
        return feignMarket.getPatrolHousingCleaningList(params, pager);
    }

    /**
     * @Description //TODO 根据管家巡房主编号获取巡房资料
     * @return com.sumainfor.common.util.JsonResult
     * @Param [params]
     * @Date 2019/8/15 18:19
     **/
    public JsonResult getPatrol(Map<String, Object> params) {
        JsonResult result = feignMarket.getPatrol(params);
        //不要问我为啥遍历这么多
        if (result.get("errorCode").equals("0")) {
            Map<String, Object> map = (Map<String, Object>) result.get("result");
            //如果是需要维修
            if (map.get("IfRepair").equals("1")) {
                List<Map<String, Object>> repair = (List<Map<String, Object>>) map.get("repair");
                for (Map<String, Object> singleRepair : repair) {
                    //获取设备名字
                    String artId = (String) singleRepair.get("ArtId");
                    List<Map<String, Object>> arts = (List<Map<String, Object>>) singleRepair.get("ArtIdList");
                    for (Map<String, Object> art : arts) {
                        if (art.get("ArtId").equals(artId)) {
                            singleRepair.put("ArtName", art.get("ArtName"));
                        }
                    }
                    //取出设备类目名字
                    String ArtTypeId = (String) singleRepair.get("ArtTypeId");
                    List<Map<String, Object>> ArtTypeIdList = (List<Map<String, Object>>) singleRepair.get("ArtTypeIdList");
                    for (Map<String, Object> artTypeId : ArtTypeIdList) {
                        if (artTypeId.get("ArtTypeId").equals(ArtTypeId)) {
                            singleRepair.put("ArtTypeName", artTypeId.get("type"));
                        }
                    }
                    //取出区域名字
                    String HouCon = (String) singleRepair.get("HouCon");
                    List<Map<String, Object>> HouConList = (List<Map<String, Object>>) singleRepair.get("HouConList");
                    for (Map<String, Object> houConList : HouConList) {
                        if (houConList.get("HouCon").equals(HouCon)) {
                            singleRepair.put("HouConName", houConList.get("HouConName"));
                        }
                    }
                    //取出设备问题名字
                    String TroId = (String) singleRepair.get("TroId");
                    List<Map<String, Object>> TroIdList = (List<Map<String, Object>>) singleRepair.get("TroIdList");
                    for (Map<String, Object> troIdList : TroIdList) {
                        if (troIdList.get("TroId").equals(TroId)) {
                            singleRepair.put("TroRemark", troIdList.get("TroRemark"));
                        }
                    }
                }
            }
            if (map.get("IfViolation").equals("1")) {
                List<Map<String, Object>> violation = (List<Map<String, Object>>) map.get("violation");
                for (Map<String, Object> singleViolation : violation) {
                    //获取房间名字
                    String HouInfoId = (String) singleViolation.get("HouInfoId");
                    List<Map<String, Object>> HouInfoIdList = (List<Map<String, Object>>) singleViolation.get("HouInfoIdList");
                    for (Map<String, Object> houInfoId : HouInfoIdList) {
                        if (houInfoId.get("HouInfoId").equals(HouInfoId)) {
                            singleViolation.put("HouInfoIdName", houInfoId.get("HouInfoIdName"));
                        }
                    }
                    //获取违规id名字
                    String VioId = (String) singleViolation.get("VioId");
                    List<Map<String, Object>> VioIdList = (List<Map<String, Object>>) singleViolation.get("VioIdList");
                    for (Map<String, Object> vioIdList : VioIdList) {
                        if (vioIdList.get("VioId").equals(VioId)) {
                            singleViolation.put("vioContent", vioIdList.get("Content"));
                        }
                    }
                    //获取违规详情名称
                    String VioInfoId = (String) singleViolation.get("VioInfoId");
                    List<Map<String, Object>> VioInfoIdList = (List<Map<String, Object>>) singleViolation.get("VioInfoIdList");
                    for (Map<String, Object> vioInfoIdList : VioInfoIdList) {
                        if (vioInfoIdList.get("VioInfoId").equals(VioInfoId)) {
                            singleViolation.put("vioInfoContent", vioInfoIdList.get("Content"));
                        }
                    }

                }
            }
            return result.put(map);

        } else {
            return result;
        }
    }

    /**
     * @Description //TODO 服务管家修改巡房记录
     * @return com.sumainfor.common.util.JsonResult
     * @Param [params]
     * @Date 2019/8/22 17:33
     **/
    public JsonResult updateLookOver(Map<String, Object> params) {
        return feignMarket.UpdatePatrol(params);
    }
}
